package com.laxcen.basiclib.core.record;

import com.laxcen.basiclib.core.downtime.DownTimer;
import com.laxcen.basiclib.core.downtime.DownTimerListener;
import com.laxcen.basiclib.utils.DateUtil;

import java.util.Date;

public class AudioRecordManager {

    private AudioRecorder audioRecorder;
    private DownTimer downTimer;
    private AudioCallback audioCallback;
    private long allTime = 30*1000;
    private long loseTime=0 ;
    public AudioRecordManager(AudioCallback audioCallback,int time){
        this.audioCallback = audioCallback;
        if(time>0){
            this.allTime = time*1000;
        }
        audioRecorder = AudioRecorder.getInstance();
        downTimer = new DownTimer();
        downTimer.setListener(new DownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                loseTime=loseTime+1000;
                if(audioCallback!=null){
                    AudioInfo audioInfo = new AudioInfo();
                    audioInfo.setAction(AudioInfo.ACTION_RECORDING);
                    int progress =(int)((float)loseTime/allTime*100);
                    audioInfo.setProgress(progress);
                    audioInfo.setTime(DateUtil.toTime(loseTime));
                    audioInfo.setMaxTime((int)allTime/1000);
                    audioInfo.setMsg("录音中...");
                    audioCallback.onResult(audioInfo);
                }
                if(loseTime>=allTime){
                    downTimer.stopDown();
                    stopRecord();
                }
            }

            @Override
            public void onFinish() {

            }
        });
    }
    //秒
    public static AudioRecordManager get(AudioCallback audioCallback,int allTime){
        return new AudioRecordManager(audioCallback,allTime);
    }
    private String fileName = DateUtil.format(new Date(),"yyyyMMddhhmmss");
    public String wavPath ;
    public void startRecord(){
        if(audioRecorder.getStatus()== AudioRecorder.Status.STATUS_PAUSE){
            pauseRecord();
            return;
        }
        if(audioRecorder.getStatus()!= AudioRecorder.Status.STATUS_NO_READY){
            AudioInfo audioInfo = new AudioInfo();
            audioInfo.setAction(AudioInfo.ACTION_ERROR);
            audioInfo.setTime(DateUtil.toTime(loseTime));
            audioInfo.setMaxTime((int)allTime/1000);
            int progress =(int)((float)loseTime/allTime*100);
            audioInfo.setProgress(progress);
            audioInfo.setMsg("已在录音,请等待录音停止或手动操作停止。");
            audioCallback.onResult(audioInfo);
            return;
        }
        if(loseTime==allTime){
            AudioInfo audioInfo = new AudioInfo();
            audioInfo.setAction(AudioInfo.ACTION_ERROR);
            audioInfo.setTime(DateUtil.toTime(loseTime));
            audioInfo.setMsg("已达到最大录音时长");
            audioInfo.setMaxTime((int)allTime/1000);
            audioCallback.onResult(audioInfo);
            return;
        }
        if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_NO_READY) {
            //初始化录音
            audioRecorder.createDefaultAudio(fileName);
            audioRecorder.startRecord(null);
            downTimer.startDown(allTime,1000);
        }else{
            pauseRecord();
        }
    }
    public void pauseRecord(){
        if(audioRecorder.getStatus()==AudioRecorder.Status.STATUS_PAUSE){
            audioRecorder.startRecord(null);
            downTimer.startDown(allTime,1000);
        }else if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_START) {
            //暂停录音
            audioRecorder.pauseRecord();
            downTimer.stopDown();
        }
    }
    public void stopRecord(){
        stopRecord(true);
    }
    public void stopRecord(boolean save){
        if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_NO_READY
                || audioRecorder.getStatus() == AudioRecorder.Status.STATUS_READY) {
            if(audioCallback!=null){
                AudioInfo audioInfo = new AudioInfo();
                audioInfo.setAction(AudioInfo.ACTION_ERROR);
                audioInfo.setMsg("录音未开始");
                audioInfo.setMaxTime((int)allTime/1000);
                audioCallback.onResult(audioInfo);
            }
            return;
        }
        if(audioRecorder.getStatus() != AudioRecorder.Status.STATUS_STOP){
            downTimer.stopDown();
            audioRecorder.stopRecord();
            audioRecorder.release();
            AudioInfo audioInfo = new AudioInfo();
            wavPath = FileUtil.getWavFileAbsolutePath(fileName);
            if(save) {
                audioInfo.setAction(AudioInfo.ACTION_COMPLETE);
                audioInfo.setTime(DateUtil.toTime(loseTime));
                audioInfo.setMsg("录音完成");
                audioInfo.setProgress(100);
                audioInfo.setPath(wavPath);
            }else{
                audioInfo.setAction(AudioInfo.ACTION_CANCEL);
                audioInfo.setMsg("录音取消");
                FileUtil.removeFile(wavPath);
            }
            loseTime=0;
            audioInfo.setMaxTime((int)allTime/1000);
            audioCallback.onResult(audioInfo);
        }

    }
}
